<template>
  <a-modal
      :title="!dataForm.id ? '新增' : '修改'"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      okText="保存"
      cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="dataForm" :model="dataForm" :rules="validatorRules">
        <a-form-model-item label="菜单类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
          <a-radio-group v-model="dataForm.type">
            <a-radio :value="0">菜单</a-radio>
            <a-radio :value="1">按钮</a-radio>
          </a-radio-group>
        </a-form-model-item>

        <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            :label="dataForm.type === 0 ? '菜单名称' : '按钮名称'"
            prop="name"
            hasFeedback>
          <a-input placeholder="请输入名称" v-model="dataForm.name" />
        </a-form-model-item>

        <a-form-model-item
            label="上级菜单"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="pid"
            :hasFeedback="true">
          <a-tree-select
              style="width:100%"
              v-model="dataForm.pid"
              :treeData="treeData"
              :replace-fields="replaceFields"
              placeholder="请选择父级菜单">
          </a-tree-select>
        </a-form-model-item>

        <a-form-model-item
            v-if="dataForm.type === 0"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="url"
            label="菜单路由">
          <a-input placeholder="请输入菜单路由" v-model="dataForm.url" />
        </a-form-model-item>

        <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="permissions"
            label="授权标识">
          <a-input placeholder="请输入授权标识" v-model="dataForm.permissions" />
        </a-form-model-item>

        <a-form-model-item
            v-if="dataForm.type === 0"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="菜单图标"
            prop="icon">
          <a-input placeholder="点击选择图标" v-model="dataForm.icon">
            <a-icon slot="addonAfter" type="setting" @click="selectIcons" />
          </a-input>
        </a-form-model-item>

        <a-form-model-item
            v-if="dataForm.type === 0"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="是否隐藏"
            prop="hidden">
          <a-switch :checked="dataForm.hidden === 1" @change="onChangeSwitch"
                    checked-children="是" un-checked-children="否"/>
        </a-form-model-item>

        <a-form-model-item
            style="width:100%"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="sort"
            :label="dataForm.type === 0 ? '菜单排序' : '按钮排序'">
          <a-input-number placeholder="请输入排序" v-model="dataForm.sort" />
        </a-form-model-item>

      </a-form-model>
    </a-spin>

    <!-- 选择图标 -->
    <icons @choose="handleIconChoose" @close="handleIconCancel" :iconChooseVisible="iconChooseVisible"></icons>

  </a-modal>
</template>

<script>
import { queryMenuTreeList, queryMenuById } from '@/api/api'
import { saveAction } from '@/api/manage'
import Icons from './icon/Icons'

export default {
  name: "menu-form",
  components: {
    Icons
  },
  data () {
    return {
      visible: false,
      confirmLoading: false,
      dataForm: {
        id: '',
        pid: 0,
        name: '',
        type: 0,
        icon: '',
        url: '',
        permissions: '',
        hidden: 0,
        sort: 0,
      },
      treeData: [],
      replaceFields: {
        children: 'children',
        title: 'name',
        key: 'id',
        value: 'id'
      },
      iconChooseVisible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      url: {
        save: '/sys/menu'
      }
    }
  },
  computed: {
    validatorRules () {
      return {
        name: [{ required: true, message: '请输入名称!', trigger: 'blur' }],
        type: [{ required: true, message: '请选择菜单类型!', trigger: 'change' }],
        pid: [{ required: true, message: '请选择上级菜单!', trigger: 'change' }],
        url: [{ required: this.dataForm.type === 0 && this.dataForm.pid !== 0, message: '请输入菜单路由!', trigger: 'blur' }],
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        Promise.all([
          this.getMenuList(),
        ]).then(() => {
          if (this.dataForm.id) {
            this.getById()
          }
        })
      })
    },
    onChangeSwitch(value) {
      this.dataForm.hidden = value ? 1: 0
    },
    getById () {
      queryMenuById(this.dataForm.id).then((res) => {
        if (res.success) {
          this.dataForm = res.result
        }
      })
    },
    // 获取菜单列表
    getMenuList () {
      return new Promise((resolve, reject) => {
        queryMenuTreeList().then((res) => {
          if (res.success) {
            this.treeData = res.result
          }
          this.treeData.unshift({ id: 0, name: '一级菜单' })
          resolve()
        }).catch(() => {
          reject()
        })
      })
    },
    handleOk () {
      const that = this;
      // 触发表单验证
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          saveAction(this.url.save, that.dataForm).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok');
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.handleCancel();
          });
        }
      })
    },
    handleCancel () {
      this.visible = false
    },
    selectIcons () {
      this.iconChooseVisible = true
    },
    handleIconCancel () {
      this.iconChooseVisible = false
    },
    handleIconChoose (value) {
      this.dataForm.icon = value
      this.iconChooseVisible = false
    },
  }
}
</script>

<style scoped>

</style>
